/**
 * Created by root on 17-7-31.
 */
var schedule = require('node-schedule');
var http = require('../utils/http');
var logger = require('../config/log4js');
var ipListService = require('../service/ipListService');

var job = null;

module.exports = {
    start : function () {
        var rule = new schedule.RecurrenceRule();
        rule.minute = [0, 15, 30, 45];
        job = schedule.scheduleJob(rule, function(){
            logger.access.info('verifyHttpTask定时任务: ' + new Date());
            ipListService.getIPByStatus(1, function (result) {
                var ipList = result;
                ipList.forEach(() => {
                    var ipList = ipListResult;
                    ipList.forEach((ip) => {
                        http.verifyHttp(ip.ip, ip.port, function (httpResult) {
                            if (httpResult.code === 200) {
                                ipListService.updateByIPAndPort({ip : ip.ip, port : ip.port, status : 1, validateCount : 0}, function () {});
                            } else {
                                ipListService.updateByIPAndPort({ip : ip.ip, port : ip.port, status : 0, validateCount : (ip.validate_count + 1)}, function () {});
                            }
                        });
                    });
                });
            });

        });
    },
    stop : function () {
        job.cancel();
    }
}
